<template>
    <div>
        <div class="filter-container">
            <el-form label-position="left" :inline="true">
                <el-form-item label="渠道名称">
                    <el-input class="filter-item" clearable v-model="search.name" placeholder="请输入渠道名称" @keyup.enter.native="pageNo = 1;handleSearch()"></el-input>
                </el-form-item>
                <el-form-item label="系统类型">
                    <select-os v-model="search.os"></select-os>
                </el-form-item>
                <el-form-item label="渠道类型">
                    <el-select v-model="search.type" clearable class="filter-item" placeholder="渠道类型" style="width: 120px;">
                        <el-option
                            v-for="(item, index) in options.type"
                            :key="index"
                            :label="item.label"
                            :value="item.value">
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="状态">
                    <el-select v-model="search.status" clearable class="filter-item" placeholder="状态筛选" style="width: 120px;">
                        <el-option
                            v-for="(item, index) in options.status"
                            :key="index"
                            :label="item.label"
                            :value="item.value">
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="">
                    <el-button class="filter-item" icon="el-icon-search" type="primary" :loading="loading" @click="pageNo = 1;handleSearch()">搜索</el-button>
                    <el-button class="filter-item" icon="el-icon-plus" type="primary" @click="create" plain>添加渠道</el-button>
                </el-form-item>
            </el-form>
        </div>

        <vxe-table
            v-loading.body="loading"
            ref="multipleTable"
            :data="tableData"
            style="width: 100%"
            class="text-center"
            @checkbox-all="selectChangeEvent"
            @checkbox-change="selectChangeEvent">
            <vxe-table-column type="checkbox" width="40" fixed="left"/>
            <vxe-table-column title="渠道编号" field="id" width="80"/>
            <vxe-table-column title="渠道类型" field="type" formatter="formatAppSourceType" width="100"/>
            <vxe-table-column title="系统类型" field="os" width="80">
                <template slot-scope="scope">
                    <tag-os :data="scope.row.os"></tag-os>
                </template>
            </vxe-table-column>
            <vxe-table-column title="渠道名" field="name" width="120"/>
            <vxe-table-column title="状态" field="status" width="80">
                <template slot-scope="scope">
                    <boolean-icon :data="scope.row.status"></boolean-icon>
                </template>
            </vxe-table-column>
            <vxe-table-column title="注册" field="register" width="80">
                <template slot-scope="scope">
                    <el-switch v-model="scope.row.register" :active-value="1" :inactive-value="0" @change="update($event, 'register', scope.row.id)"/>
                </template>
            </vxe-table-column>
            <vxe-table-column title="审核模式" field="audit" width="80">
                <template slot-scope="scope">
                    <el-switch v-model="scope.row.audit" :active-value="1" :inactive-value="0" @change="update($event, 'audit', scope.row.id)"/>
                </template>
            </vxe-table-column>
            <vxe-table-column title="数据上报" field="data_report" width="80">
                <template slot-scope="scope">
                    <el-switch v-model="scope.row.data_report" :active-value="1" :inactive-value="0" @change="update($event, 'data_report', scope.row.id)"/>
                </template>
            </vxe-table-column>
            <vxe-table-column title="渠道方案" field="app_source_config_id" min-width="180">
                <template slot-scope="scope">
                    <el-link
                        :href="$router.resolve({name: 'appSource.config', query:{id: scope.row.app_source_config_id}}).href"
                        title="点击跳转">
                        {{ scope.row.config ? scope.row.config.id+' ) '+scope.row.config.name : '-' }}
                    </el-link>
                </template>
            </vxe-table-column>
            <vxe-table-column title="介绍" field="desc" show-tooltip-overflow/>
            <vxe-table-column title="创建时间" field="created_at" width="140"/>
            <vxe-table-column title="更新时间" field="updated_at" width="140"/>
            <vxe-table-column title="操 作" width="150px">
                <template slot-scope="scope">
                    <el-button
                        type="primary"
                        @click="edit(scope.row)">
                        编辑
                    </el-button>
                    <destroy-confirm
                        route="appSource/Destroy"
                        :primary="scope.row.id"
                        @success="onDestroy">
                    </destroy-confirm>
                </template>
            </vxe-table-column>
        </vxe-table>

        <div class="mt-10 pull-left">
            <destroy-multiple
                route="appSource/DestroyMultiple"
                :selected="selected"
                @success="onDestroy">
                <el-button type="danger"  plain>删除选中</el-button>
            </destroy-multiple>
        </div>

        <el-pagination
            class="pull-right"
            background
            :current-page.sync="pageNo"
            :page-sizes="pageOptions.pageSizesList"
            :page-size="pageSize"
            :layout="pageOptions.layout"
            :total="totalDataNumber"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange">
        </el-pagination>

        <el-dialog
            :title="form.data && form.data.id ? '编辑' : '添加'"
            :visible.sync="form.show"
            width="30%"
            :append-to-body="true"
            :close-on-click-modal="false"
            center>
            <dialog-form
                :data="form.data"
                v-on:submit="onFormSubmit"
                v-on:cancel="form.show = false">
            </dialog-form>
        </el-dialog>

    </div>
</template>

<script>
import dialogForm from './form'
import TagOs from '~/components/Tag/os'
import TagAppSourceType from '~/components/Tag/app-source-type'
// import TagAppSourceConfig from '~/components/Tag/app-source-config'
export default {
    components: {
        dialogForm,
        TagOs,
        TagAppSourceType,
        // TagAppSourceConfig,
    },
    data() {
        return {
            search: {
                name: '',
                os: '',
                type: '',
                status: '',
            },
            loading: false,
            selected: [],
            tableData: [],
            pageNo: 1,
            pageSize: 20,
            pageOptions: this.$store.state.backend.pageOptions,
            totalDataNumber: 0,
            options: this.$store.state.appSource.options,
            form: {
                data: {},
                show: false,
            }
        }
    },
    methods: {
        selectChangeEvent ({ checked, records }) {
            this.selected = records.map(v => v.id);
            // console.log(checked ? '勾选事件' : '取消事件', records)
        },
        handleSizeChange(val) {
            this.pageNo = 1;
            this.pageSize = parseInt(`${val}`);
            this.handleSearch();
        },
        handleCurrentChange(val) {
            this.pageNo = parseInt(`${val}`);
            this.handleSearch();
        },
        handleSearch() {
            this.loading = true;
            let filter = Object.assign({}, this.search, {
                page: this.pageNo,
                pageSize: this.pageSize,
            });
            this.$store.dispatch('appSource/Lists', filter).then(res => {
                this.tableData = res.data.data;
                this.totalDataNumber = res.data.meta.total;
            }).finally(() => {
                this.loading = false;
            });
        },
        initData() {
            this.handleSearch();
        },
        create() {
            this.form.data = {};
            this.form.show = true;
        },
        edit(row) {
            this.form.data = row;
            this.form.show = true;
        },
        onFormSubmit() {
            this.form.show = false;
            this.handleSearch();
        },
        onDestroy() {
            // 更新缓存
            this.$store.dispatch('appSource/All', 'appSource');
            this.handleSearch();
        },
        update(val, field, id) {
            let obj = {
                id: id,
            };
            obj[field] = val;
            this.loading = true;
            this.$store.dispatch('appSource/Update', obj).then(res => {
                this.$message.success('成功');
            }).finally(() => {
                this.loading = false;
            })
        },
    },
    created() {
        this.initData();
    }
}
</script>

<style scoped>

</style>

